#include "configmanager.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

ConfigManager::ConfigManager(QObject *parent) : QObject(parent)
{
}

bool ConfigManager::loadConfig(const QString &configPath)
{
    QFile configFile(configPath);
    if (!configFile.open(QIODevice::ReadOnly)) {
        qWarning() << "无法打开配置文件:" << configPath;
        return false;
    }

    QByteArray configData = configFile.readAll();
    configFile.close();

    QJsonParseError parseError;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(configData, &parseError);

    if (parseError.error != QJsonParseError::NoError) {
        qWarning() << "配置文件解析错误:" << parseError.errorString();
        return false;
    }

    if (!jsonDoc.isObject()) {
        qWarning() << "配置文件格式错误: 根元素不是JSON对象";
        return false;
    }

    QJsonObject rootObj = jsonDoc.object();

    // 解析筛选器配置
    if (rootObj.contains("filters") && rootObj["filters"].isArray()) {
        if (!parseFilters(rootObj["filters"].toArray())) {
            return false;
        }
    } else {
        qWarning() << "配置文件缺少filters数组或格式错误";
        return false;
    }

    // 解析列配置
    if (rootObj.contains("columns") && rootObj["columns"].isArray()) {
        if (!parseColumns(rootObj["columns"].toArray())) {
            return false;
        }
    } else {
        qWarning() << "配置文件缺少columns数组或格式错误";
        return false;
    }

    // 解析分页配置
    if (rootObj.contains("pagination") && rootObj["pagination"].isObject()) {
        if (!parsePagination(rootObj["pagination"].toObject())) {
            return false;
        }
    } else {
        qWarning() << "配置文件缺少pagination对象或格式错误";
        return false;
    }

    return true;
}

bool ConfigManager::parseFilters(const QJsonArray &filtersArray)
{
    m_filters.clear();

    for (int i = 0; i < filtersArray.size(); ++i) {
        if (!filtersArray[i].isObject()) {
            qWarning() << "筛选器配置必须是对象";
            continue;
        }

        QJsonObject filterObj = filtersArray[i].toObject();
        FilterConfig filter;

        if (filterObj.contains("name") && filterObj["name"].isString()) {
            filter.name = filterObj["name"].toString();
        } else {
            qWarning() << "筛选器缺少name字段或格式错误";
            continue;
        }

        if (filterObj.contains("type") && filterObj["type"].isString()) {
            filter.type = filterObj["type"].toString();
        } else {
            qWarning() << "筛选器缺少type字段或格式错误";
            continue;
        }

        if (filterObj.contains("field") && filterObj["field"].isString()) {
            filter.field = filterObj["field"].toString();
        } else {
            qWarning() << "筛选器缺少field字段或格式错误";
            continue;
        }

        if (filterObj.contains("options") && filterObj["options"].isArray()) {
            QJsonArray optionsArray = filterObj["options"].toArray();
            for (int j = 0; j < optionsArray.size(); ++j) {
                if (optionsArray[j].isString()) {
                    filter.options.append(optionsArray[j].toString());
                }
            }
        }

        m_filters.append(filter);
    }

    return true;
}

bool ConfigManager::parseColumns(const QJsonArray &columnsArray)
{
    m_columns.clear();

    for (int i = 0; i < columnsArray.size(); ++i) {
        if (!columnsArray[i].isObject()) {
            qWarning() << "列配置必须是对象";
            continue;
        }

        QJsonObject columnObj = columnsArray[i].toObject();
        ColumnConfig column;

        if (columnObj.contains("name") && columnObj["name"].isString()) {
            column.name = columnObj["name"].toString();
        } else {
            qWarning() << "列配置缺少name字段或格式错误";
            continue;
        }

        if (columnObj.contains("field") && columnObj["field"].isString()) {
            column.field = columnObj["field"].toString();
        } else {
            qWarning() << "列配置缺少field字段或格式错误";
            continue;
        }

        if (columnObj.contains("width") && columnObj["width"].isDouble()) {
            column.width = columnObj["width"].toInt();
        } else {
            column.width = 100; // 默认宽度
        }

        if (columnObj.contains("filterable") && columnObj["filterable"].isBool()) {
            column.filterable = columnObj["filterable"].toBool();
        } else {
            column.filterable = false; // 默认不可筛选
        }

        if (columnObj.contains("filterType") && columnObj["filterType"].isString()) {
            column.filterType = columnObj["filterType"].toString();
        }

        if (columnObj.contains("filterOptions") && columnObj["filterOptions"].isArray()) {
            QJsonArray optionsArray = columnObj["filterOptions"].toArray();
            for (int j = 0; j < optionsArray.size(); ++j) {
                if (optionsArray[j].isString()) {
                    column.filterOptions.append(optionsArray[j].toString());
                }
            }
        }

        if (columnObj.contains("sortable") && columnObj["sortable"].isBool()) {
            column.sortable = columnObj["sortable"].toBool();
        } else {
            column.sortable = false; // 默认不可排序
        }

        if (columnObj.contains("interactive") && columnObj["interactive"].isBool()) {
            column.interactive = columnObj["interactive"].toBool();
        } else {
            column.interactive = false; // 默认不可交互
        }

        m_columns.append(column);
    }

    return true;
}

bool ConfigManager::parsePagination(const QJsonObject &paginationObject)
{
    if (paginationObject.contains("defaultPageSize") && paginationObject["defaultPageSize"].isDouble()) {
        m_paginationConfig.defaultPageSize = paginationObject["defaultPageSize"].toInt();
    } else {
        m_paginationConfig.defaultPageSize = 10; // 默认每页10条
    }

    if (paginationObject.contains("pageSizeOptions") && paginationObject["pageSizeOptions"].isArray()) {
        QJsonArray optionsArray = paginationObject["pageSizeOptions"].toArray();
        for (int i = 0; i < optionsArray.size(); ++i) {
            if (optionsArray[i].isDouble()) {
                m_paginationConfig.pageSizeOptions.append(optionsArray[i].toInt());
            }
        }
    } else {
        // 默认页大小选项
        m_paginationConfig.pageSizeOptions = {10, 20, 50, 100};
    }

    return true;
}

QVector<FilterConfig> ConfigManager::getFilters() const
{
    return m_filters;
}

QVector<ColumnConfig> ConfigManager::getColumns() const
{
    return m_columns;
}

PaginationConfig ConfigManager::getPaginationConfig() const
{
    return m_paginationConfig;
}
